package com.badibadi.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.badibadi.activity.RegionHomePageActivity;
import com.badibadi.infos.AreaListModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MySharePreferences;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.TempTools;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.view.my_view.XListView;
import com.view.my_view.XListView.IXListViewListener;
import com.libjph.util.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class IndexClub3Fragment extends BaseFragment {
	
	private String img="http://photo.uniclubber.com/Public/Uploads/2014-10/141416150316618.jpg!imgshow";

	private XListView1Adapter_1_2 x1Adapter;
	private ListView xListView1;
	private List<AreaListModel> areaListModels;
	private MySharePreferences preferences;
	private int languageType = 0;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				try {
					Utils.ExitPrgress(getActivity());
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 2:
				try {
					Utils.ExitPrgress(getActivity());
					x1Adapter = new XListView1Adapter_1_2(areaListModels,
							getActivity());
					xListView1.setAdapter(x1Adapter);
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 3:
				try {
					Utils.ExitPrgress(getActivity());
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.l_xa10));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;

			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		areaListModels = new ArrayList<AreaListModel>();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View view = inflater.inflate(R.layout.after_landing_friends00_no_xiala,
				null);
		preferences = new MySharePreferences(getActivity(), "language");
		languageType = preferences.get("language", 0);
		Show_hot_area();
		InitMyXListView1(view);
		return view;
	}

	public void InitMyXListView1(View view) {
		xListView1 = (ListView) view
				.findViewById(R.id.after_landing_friends00_xlistView_no_xiala);
		xListView1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				if (areaListModels.get(position).getArea_id() != null
						&& !areaListModels.get(position).getArea_id()
								.equals("null")) {
					Intent intent = new Intent(getActivity(),
							RegionHomePageActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.putExtra("area_id", areaListModels.get(position)
							.getArea_id());
					getActivity().startActivity(intent);
				} else {
					Toast.makeText(getActivity(),
							getResources().getString(R.string.wx_txt_62), 0)
							.show();
				}
			}
		});
		// xListView1.startRefresh();
	}

	private void Show_hot_area() {
		// Utils.showPrgress(getActivity());
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				if (areaListModels != null) {
					areaListModels.clear();
				}
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("languageType", TempTools.BackLanguage(languageType));
				String cluecontent = Utils.sendRequest(map, Constants.BadiUrl
						+ "show_hot_area");
				if (cluecontent != null) {
					Results results = Utils.checkResult_NNN(getActivity(),
							cluecontent);
					if (results != null && results.getRetmsg() != null
							&& results.isRet()) {
						//    ("热门地区数据结果是:" +
						// results.getRetmsg());
//						   
						try {
							areaListModels = JSONUtils.getListByJsonString(
									results.getRetmsg(), AreaListModel.class);
							handler.sendEmptyMessage(2);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	class XListView1Adapter_1_2 extends BaseAdapter {
		private List<AreaListModel> areaListModels;
		private Context context;
		private DisplayImageOptions options;

		public XListView1Adapter_1_2(List<AreaListModel> areaListModels,
				Context context) {
			this.context = context;
			this.areaListModels = areaListModels;
			options = new DisplayImageOptions.Builder()
					.showStubImage(R.drawable.ic_stub)
					.showImageForEmptyUri(R.drawable.noimage)
					.showImageOnFail(R.drawable.noimage).cacheInMemory()
					.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5))
					.build();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return areaListModels.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return areaListModels.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub
			LayoutInflater inflater = LayoutInflater.from(getActivity());
			convertView = inflater.inflate(R.layout.fragment_index_club3_1,
					null);
			ImageView area_item_img = (ImageView) convertView
					.findViewById(R.id.area_list_item_img);
			TextView area_item_name = (TextView) convertView
					.findViewById(R.id.area_list_item_name);
			TextView area_item_num = (TextView) convertView
					.findViewById(R.id.area_list_item_num);
			TextView area_list_item_details = (TextView) convertView
					.findViewById(R.id.area_list_item_details);
			try {
				area_item_name.setText(areaListModels.get(position)
						.getArea_name());
				area_item_num.setText(""
						+ areaListModels.get(position).getClubCount());
				if(!StringUtil.isNullOrEmpty(areaListModels.get(position).getArea_img()))
				{
					ImageLoader.getInstance().displayImage(
							Constants.BadiDownImgUrl
									+ areaListModels.get(position).getArea_img()
									+ Constants.appPhoto4img, area_item_img,
							options);
				}
				else{
					ImageLoader.getInstance().displayImage(
							img, area_item_img,
							options);
				}
				
				
				area_list_item_details
						.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View arg0) {
								// Toast.makeText(
								// context,
								// "查看" +
								// areaListModels.get(position).getArea_name()
								// + "周围的详情", 0).show();
								if (areaListModels.get(position).getArea_id() != null
										&& !areaListModels.get(position)
												.getArea_id().equals("null")) {
									Intent intent = new Intent(context,
											RegionHomePageActivity.class);
									intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
									intent.putExtra("area_id", areaListModels
											.get(position).getArea_id());
									context.startActivity(intent);
								} else {
									Toast.makeText(
											context,
											getResources().getString(
													R.string.wx_txt_62), 0)
											.show();
								}
							}
						});
			} catch (Exception e) {
				   
			}

			return convertView;
		}

	}

	// @Override
	// public void onListViewRefresh() {
	// // TODO Auto-generated method stub
	// Show_hot_area();
	// }
	//
	// @Override
	// public void onListViewLoadMore() {
	// Show_hot_area();
	// }
}